
// 重新封装 forEach
Array.prototype.myForEach = async function (cb, thisArg) {
  const _arr = this,
        _isArray = Array.isArray(_arr),
        _thisArg = Object(thisArg) || window;

  if (!_isArray)
    throw new TypeError('The caller of myForEach must be the type "Array"');
  
  for (let i = 0; i < _arr.length; i ++) {
    await cb.call(_thisArg, _arr[i], i, _arr);
  }

}


// 问: 

fun([
  () => console.log('start'),
  () => sleep(1000),
  () => console.log('1'),
  () => sleep(2000),
  () => console.log('2'),
  () => sleep(3000),
  () => console.log('end')
])

// -------------------------------

function sleep(ms) {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, ms);
  })
}

function fun(arr) {

  // 问2：解决forEach 的异步问题
  // arr.forEach(async fn => {
  //   await fn();
  // })

  arr.myForEach(async fn => {
    await fn();
  })

  // for(let i = 0; i < arr.length; i ++) {
  //   await arr[i]();
  // }
}




// async function test () {
//   await xxx
//   await xxx
//   await xxx
// }

// async function test2 () {}

